package com.activity;

import com.misc.Tool;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import java.io.DataOutputStream;
import java.net.Socket;

public class CreateTopicActivity extends Activity {
	// constant
	final int SEND_CREATED_TOPIC = 202;
	
	// field
	EditText etTitle, etContent;
	Button btnPhoto, btnDone;
	
	boolean isTitleFilled = false, isContentFilled = false;
	String myPhNum;
	Bitmap bitmap;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_topic_activity);
		
		myPhNum = getIntent().getStringExtra(MainActivity.MY_PH_NUM);
		bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.activity_photo_default_empty)).getBitmap();
		
		etTitle = (EditText) findViewById(R.id.etTitle);
		etContent = (EditText) findViewById(R.id.etContent);
		btnDone = (Button) findViewById(R.id.btnDone);
		
		etTitle.addTextChangedListener(new TextWatcher() {
	        public void onTextChanged(CharSequence s, int start, int before, int count) {
	        	isTitleFilled = !TextUtils.isEmpty(s);
				btnDone.setEnabled(isTitleFilled && isContentFilled);
	        }
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	        }
	        public void afterTextChanged(Editable s) {
	        }
	    });
		etContent.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				isContentFilled = !TextUtils.isEmpty(s);
				btnDone.setEnabled(isTitleFilled && isContentFilled);
			}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			public void afterTextChanged(Editable s) {
			}
		});
		
		btnPhoto = (Button) findViewById(R.id.btnPhoto);
		btnPhoto.setHintTextColor(Color.BLUE);
		btnPhoto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.setType("image/*");
				intent.putExtra("return-data", true);
				startActivityForResult(intent, 0);
			}
		});
		
		btnDone.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				(new SendCreatedTopicTask()).execute(
												new ParamForSendCreatedTopicTask(
														MainActivity.SERVER_IP,
														Integer.parseInt(MainActivity.PORT), 
														myPhNum,
														etTitle.getText().toString(),
														etContent.getText().toString(), 
														Tool.bitmap2ByteArray(bitmap)));
				finish();
			}
		});
		((Button) findViewById(R.id.btnCancel)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0 && intent != null) {
			bitmap = intent.getParcelableExtra("data");
			Matrix matrix = new Matrix();
			matrix.postScale(150 / (float) bitmap.getWidth(), 130 / (float) bitmap.getHeight());
			btnPhoto.setBackgroundDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true)));
			btnPhoto.setHint("");
		}
	}
	
	
	class ParamForSendCreatedTopicTask {
		String serverIp;
		int port;
		String myPhNum;
		String title;
		String content;
		byte[] photo;
		public ParamForSendCreatedTopicTask(String serverIp, int port, String myPhNum, String title, String content, byte[] photo) {
			this.serverIp = serverIp;
			this.port = port;
			this.myPhNum = myPhNum;
			this.title = title;
			this.content = content;
			this.photo = photo;
		}
		public String getServerIp() {
			return serverIp;
		}
		public int getPort() {
			return port;
		}
		public String getMyPhNum() {
			return myPhNum;
		}
		public String getTitle() {
			return title;
		}
		public String getContent() {
			return content;
		}
		public byte[] getPhoto() {
			return photo;
		}
	}
	class SendCreatedTopicTask extends AsyncTask<ParamForSendCreatedTopicTask, Void, Void> {
	    @Override
		protected Void doInBackground(ParamForSendCreatedTopicTask... params) {
	    	Socket socket = null;
	    	DataOutputStream dos = null;	
	    	try {
				socket = new Socket(params[0].getServerIp(), params[0].getPort());
				dos = new DataOutputStream(socket.getOutputStream());
				dos.writeInt(SEND_CREATED_TOPIC);
				
				dos.writeUTF(params[0].getMyPhNum());
				dos.writeUTF(params[0].getTitle());
				dos.writeUTF(params[0].getContent());
				Tool.sendByteArray(dos, params[0].getPhoto());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					dos.close();
					socket.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return null;
		}
	}
}
